package wxusersocket.controller;

import config.Interceptor.PassToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import utils.CommonRetumType;
import utils.RedisUtil;
import wxusersocket.entity.Wxusersocket;
import wxusersocket.service.impl.Wxusersocketserviceimpl;

import java.util.*;

@RestController
@RequestMapping("/Wxusersocket")
public class Wxusersocketcontroller{
    @Autowired
    private Wxusersocketserviceimpl wxusersocketserviceimpl;
    @Autowired
    RedisUtil redisUtil;


    @GetMapping("/create")
    @PassToken
    public CommonRetumType create(@RequestParam("openId") String openid,
                                  @RequestParam("lastopenid") String lastopenid,
                                  @RequestParam("name") String name,
                                  @RequestParam("url") String url
                                   )
    {
        String openlast = openid+"#"+lastopenid;
        Wxusersocket wxusersocket = new Wxusersocket();
        wxusersocket.setOpenid(openid);
        wxusersocket.setLastopenid(lastopenid);
        wxusersocket.setMessagepid(openid+"#"+lastopenid);
        wxusersocket.setMessage(null);
        Wxusersocket wxusersocketList = wxusersocketserviceimpl.selectBymessagepid(openlast);
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("name",name);
        hashMap.put("url",url);
        if (wxusersocketList!=null)
        {
            return CommonRetumType.create(0,"信息已存在");
        }else {

            redisUtil.hmset(openlast,hashMap);

            return CommonRetumType.create(wxusersocketserviceimpl.insertSelective(wxusersocket),"交友信息添加成功");

        }
    }

    @GetMapping("/index")
    @PassToken
    public CommonRetumType index(@RequestParam("openId") String openid)
    {

        List<Wxusersocket> wxusersocketList = wxusersocketserviceimpl.selectByopenid(openid);
        HashMap<String,Object> hashMap = new HashMap<>();
        for (Wxusersocket wxusersocket:wxusersocketList) {

            String openlast = openid+"#"+wxusersocket.getLastopenid();
            wxusersocket.setRedisentity(redisUtil.hmget(openlast));
            hashMap.put(openlast,wxusersocket);
        }
        return CommonRetumType.create(hashMap,"交友查询成功");
    }


    @GetMapping("/delete")
    @PassToken
    public CommonRetumType delete(@RequestParam("openId") String openid,
                                  @RequestParam("lastopenid") String lastopenid)
    {
        String usermessgeid = openid+"#"+lastopenid;
        String useropenid = lastopenid+"#"+openid;

        redisUtil.sSet("list",usermessgeid);
        redisUtil.sSet("list",useropenid);
        wxusersocketserviceimpl.deleteByUser(usermessgeid);
        return CommonRetumType.create(wxusersocketserviceimpl.deleteByUser(useropenid),"删除好友关系成功");
    }

    @GetMapping("/read")
    @PassToken
    public CommonRetumType read(@RequestParam("openId") String openid,
                                @RequestParam("lastopenid") String lastopenid)
    {
        String usermessgeid = openid+"#"+lastopenid;


        if (redisUtil.sHasKey("list", usermessgeid)) {

            return CommonRetumType.create(false,"不存在好友关系");
        } else {

            return CommonRetumType.create(true,"存在好友关系");
        }

    }
}
